Reference
  Area\Util.txt
  Util\Audio.txt
  Util\CommonDialog.txt
  Util\Party.txt
  Util\Status.txt
  Util\VisualEffect.txt
  Util\PartyStatusWindow.txt
End Reference

Procedure UpdateToolInfo(index, params)

  Dim hWnd = params[1]
  Dim who = params[2]
  Dim item = params[3][index]

  GetInfoWindowCell(hWnd, 1, 1).Text = "種類"
  If item.Part = 0 Then
    GetInfoWindowCell(hWnd, 2, 1).Text = "どうぐ"
    GetInfoWindowCell(hWnd, 1, 2).Text = ""
    GetInfoWindowCell(hWnd, 2, 2).Text = ""
    GetInfoWindowCell(hWnd, 1, 3).Text = "対象"
    If item.TargetRange = 0 Then
      GetInfoWindowCell(hWnd, 2, 3).Text = "使用者"
    ElseIf item.TargetRange = 1 Then
      GetInfoWindowCell(hWnd, 2, 3).Text = "味方単体"
    ElseIf item.TargetRange = 2 Then
      GetInfoWindowCell(hWnd, 2, 3).Text = "敵単体"
    ElseIf item.TargetRange = 3 Then
      GetInfoWindowCell(hWnd, 2, 3).Text = "味方全体"
    ElseIf item.TargetRange = 4 Then
      GetInfoWindowCell(hWnd, 2, 3).Text = "敵全体"
    ElseIf item.TargetRange = 5 Then
      GetInfoWindowCell(hWnd, 2, 3).Text = "全体"
    End If
  Else
    Dim curr_eq = Nothing
    If item.Part = 1 Then
      GetInfoWindowCell(hWnd, 2, 1).Text = "ぶ　き"
      GetInfoWindowCell(hWnd, 1, 2).Text = "Ｌｖ"
      GetInfoWindowCell(hWnd, 2, 2).Text = HanToZen(item.LevelDep[1])
      curr_eq = who.Weapon
    ElseIf item.Part = 2 Then
      GetInfoWindowCell(hWnd, 2, 1).Text = "からだ"
      GetInfoWindowCell(hWnd, 1, 2).Text = ""
      GetInfoWindowCell(hWnd, 2, 2).Text = ""
      curr_eq = who.Armor
    ElseIf item.Part = 3 Then
      GetInfoWindowCell(hWnd, 2, 1).Text = "う　で"
      GetInfoWindowCell(hWnd, 1, 2).Text = ""
      GetInfoWindowCell(hWnd, 2, 2).Text = ""
      curr_eq = who.Shield
    ElseIf item.Part = 4 Then
      GetInfoWindowCell(hWnd, 2, 1).Text = "あたま"
      GetInfoWindowCell(hWnd, 1, 2).Text = ""
      GetInfoWindowCell(hWnd, 2, 2).Text = ""
      curr_eq = who.Helmet
    End If
    Dim item_value = 0
    If item.Part = 1 Then
      item_value = item.DamageAP[1]
    Else
      item_value = item.DamageDP[1]
    End If
    GetInfoWindowCell(hWnd, 1, 3).Text = "効果"
    If curr_eq <> Nothing Then
      Dim curr_value = 0
      If item.Part = 1 Then
        curr_value = curr_eq.DamageAP[1]
      Else
        curr_value = curr_eq.DamageDP[1]
      End If
      If item_value > curr_value Then
        GetInfoWindowCell(hWnd, 2, 3).Text = HanToZen(item_value + _
          "(+" + (item_value - curr_value) + ")")
      ElseIf item_value < curr_value Then
        GetInfoWindowCell(hWnd, 2, 3).Text = HanToZen(item_value + _
          "(-" + (curr_value - item_value) + ")")
      Else
        GetInfoWindowCell(hWnd, 2, 3).Text = HanToZen(item_value + "(0)")
      End If
    Else
      GetInfoWindowCell(hWnd, 2, 3).Text = HanToZen(item_value)
    End If
  End If

  GetInfoWindowCell(hWnd, 1, 4).Text = item.Explanation
  GetInfoWindowCell(hWnd, 2, 4).Text = ""

End Procedure

Procedure DoShop(items)

  DoMsg("いらっしゃいませ")

  Dim hPrps = CreateMenu(0, 0, 120, 120, Nothing, Nothing, _
    Nothing, {"か　う", "う　る", "かえる"}, Nothing, 1)
  Dim index = ProcessMenu(hPrps)
  Do While index = 1 OrElse index = 2
    Dim member_menu = CreateNameListBox(GetPartyMembers(), 120, 0, 240, 120, _
      Nothing, Nothing, "どなたが？", 1)
    Dim member_index = ProcessMenu(member_menu)
    Do While member_index > 0
      Dim who = GetPartyMembers()[member_index]
      Dim money_info = CreateEmptyInfoWindow(30, 150, 320, 30, 2, 1)
      GetInfoWindowCell(money_info, 1, 1).Text = "所持金"
      GetInfoWindowCell(money_info, 2, 1).Text = HanToZen(CheckOurMoney())
      Dim tool_info = CreateEmptyInfoWindow(350, 180, 280, 120, 2, 4)
      If index = 1 Then 'Buy
        Dim names = CreateList()
        Dim prices = CreateList()
        For i = 1 To items.Count
          Dim item = items[i]
          names.Add(item.Name)
          prices.Add(HanToZen(item.Price))
        Next i
        Dim item_menu = CreateMenu(30, 180, 320, 240, UpdateToolInfo, {tool_info, who, items}, _
          "商品リスト", names, prices, 1)
        Dim item_index = ProcessMenu(item_menu)
        Do While item_index > 0
          Dim the_item = items[item_index]
          If HaveEnoughMoney(the_item.Price) Then
            If DoYesNoMsg(the_item.Name + "で", "よろしいですか？") = True Then
              PayMoney(the_item.Price)
              GetInfoWindowCell(money_info, 2, 1).Text = CheckOurMoney()
              who.ToolList.Add(the_item)
              DoMsg("ありがとうございました")
            End If
          Else
            DoMsg("お支払いが難しいようですね")
          End If
          item_index = ProcessMenu(item_menu)
        Loop
        DestroyControl(item_menu)
      ElseIf index = 2 Then 'Sell
        Dim tool_index = 1
        Do
          If who.ToolList.Count < tool_index Then
            tool_index -= 1
          End If
          Dim tool_menu = CreateNameListBox(who.ToolList, 30, 180, 320, 240, UpdateToolInfo, {tool_info, who, who.ToolList}, _
            "所持品リスト", tool_index)
          tool_index = ProcessMenu(tool_menu)
          If tool_index > 0 Then
            Dim the_tool = who.ToolList[tool_index]
            If Not the_tool.Importance Then
              Dim bid_rate = the_tool.Price / 2
              If DoYesNoMsg(the_tool.Name + "でしたら", MoneyStr(bid_rate) + "になりますが", "よろしいですか？") = True Then
                ReceiveMoney(bid_rate)
                GetInfoWindowCell(money_info, 2, 1).Text = CheckOurMoney()
                who.ToolList.RemoveAt(tool_index)
                DoMsg("ありがとうございました")
              End If
            Else
              DoMsg("どうか大切になさってください")
            End If
            DestroyControl(tool_menu)
          Else
            DestroyControl(tool_menu)
            Exit Do
          End If
        Loop
      End If
      DestroyControl(tool_info)
      DestroyControl(money_info)
      member_index = ProcessMenu(member_menu)
    Loop
    DestroyControl(member_menu)
    index = ProcessMenu(hPrps)
  Loop

  DestroyControl(hPrps)
  DoMsg("またのおこしを")

End Procedure

Procedure UpdateMagicInfo(index, params)

  Dim hWnd = params[1]
  Dim who = params[2]
  Dim item = params[3][index]

  GetInfoWindowCell(hWnd, 1, 1).Text = "消費"
  GetInfoWindowCell(hWnd, 2, 1).Text = HanToZen(item.Cost)

  GetInfoWindowCell(hWnd, 1, 2).Text = "Ｌｖ"
  GetInfoWindowCell(hWnd, 2, 2).Text = HanToZen(item.LevelDep[2])

  GetInfoWindowCell(hWnd, 1, 3).Text = "対象"
  If item.TargetRange = 0 Then
    GetInfoWindowCell(hWnd, 2, 3).Text = "使用者"
  ElseIf item.TargetRange = 1 Then
    GetInfoWindowCell(hWnd, 2, 3).Text = "味方単体"
  ElseIf item.TargetRange = 2 Then
    GetInfoWindowCell(hWnd, 2, 3).Text = "敵単体"
  ElseIf item.TargetRange = 3 Then
    GetInfoWindowCell(hWnd, 2, 3).Text = "味方全体"
  ElseIf item.TargetRange = 4 Then
    GetInfoWindowCell(hWnd, 2, 3).Text = "敵全体"
  ElseIf item.TargetRange = 5 Then
    GetInfoWindowCell(hWnd, 2, 3).Text = "全体"
  End If
  
  GetInfoWindowCell(hWnd, 1, 4).Text = item.Explanation
  GetInfoWindowCell(hWnd, 2, 4).Text = ""

End Procedure

Procedure DoMagicShop(items)

  DoMsg("いらっしゃいませ")

  Dim hPrps = CreateMenu(0, 0, 120, 120, Nothing, Nothing, _
    Nothing, {"か　う", "かえる"}, Nothing, 1)
  Dim index = ProcessMenu(hPrps)
  Do While index = 1
    Dim member_menu = CreateNameListBox(GetPartyMembers(), 120, 0, 240, 120, _
      Nothing, Nothing, "どなたが？", 1)
    Dim member_index = ProcessMenu(member_menu)
    Do While member_index > 0
      Dim who = GetPartyMembers()[member_index]
      Dim money_info = CreateEmptyInfoWindow(30, 150, 320, 30, 2, 1)
      GetInfoWindowCell(money_info, 1, 1).Text = "所持金"
      GetInfoWindowCell(money_info, 2, 1).Text = HanToZen(CheckOurMoney())
      Dim magic_info = CreateEmptyInfoWindow(350, 180, 280, 120, 2, 4)
      Dim names = CreateList()
      Dim prices = CreateList()
      For i = 1 To items.Count
        Dim item = items[i]
        names.Add(item.Name)
        prices.Add(HanToZen(item.Price))
      Next
      Dim item_menu = CreateMenu(30, 180, 320, 240, UpdateMagicInfo, {magic_info, who, items}, _
        "商品リスト", names, prices, 1)
      Dim item_index = ProcessMenu(item_menu)
      Do While item_index > 0
        Dim the_item = items[item_index]
        Dim learned = False
        For i = 1 To who.MagicList.Count
          If who.MagicList[i] = the_item Then
            learned = True
            Exit For
          End If
        Next
        If Not learned Then
          If HaveEnoughMoney(the_item.Price) Then
            If DoYesNoMsg(the_item.Name + "で", "よろしいですか？") = True Then
              PayMoney(the_item.Price)
              GetInfoWindowCell(money_info, 2, 1).Text = CheckOurMoney()
              who.MagicList.Add(the_item)
              DoMsg("ありがとうございました")
            End If
          Else
            DoMsg("お支払いが難しいようですね")
          End If
        Else
          DoMsg("すでにご習得されているようです")
        End If
        item_index = ProcessMenu(item_menu)
      Loop
      DestroyControl(item_menu)
      DestroyControl(magic_info)
      DestroyControl(money_info)
      member_index = ProcessMenu(member_menu)
    Loop
    DestroyControl(member_menu)
    index = ProcessMenu(hPrps)
  Loop

  DestroyControl(hPrps)
  DoMsg("またのおこしを")

End Procedure

Procedure DoInn(fee)

  Dim total = 0
  Dim max_mp = 0
  Dim mp = 0
  Dim we = GetPartyMembers()
  
  For i = 1 To we.Count
    max_mp += we[i].MaxMP
    mp += we[i].MP
  Next i
  total = we.Count * fee * (max_mp - mp) / max_mp
  If total < we.Count * fee / 10 Then total = we.Count * fee / 10
  
  If DoYesNoMsg("いらっしゃいませ", "一泊" + MoneyStr(total) + "になります。") = True Then
    If PayMoney(total) Then
      FadeInProc(0, 0, 0, 1000)
      ChangeBGM(Nothing)
      Sleep(500)
      ChangeBGM(Music.Inn)
      Sleep(6000)
      CureUsPerfectly(True)
      ChangeBGM(Music.Town)
      FadeOutProc(0, 0, 0, 500)
      DoMsg("ありがとうございました", "またのおこしを")
    Else
      DoMsg("お支払いが難しいようですね")
    End If
  End If

End Procedure
